/*     */ package com.cr.system.service.impl;
/*     */ 
/*     */ import com.cr.common.core.domain.Ztree;
/*     */ import com.cr.common.core.domain.entity.SysMenu;
/*     */ import com.cr.common.core.domain.entity.SysRole;
/*     */ import com.cr.common.core.domain.entity.SysUser;
/*     */ import com.cr.common.utils.MyStringUtils;
/*     */ import com.cr.system.mapper.SysMenuMapper;
/*     */ import com.cr.system.mapper.SysRoleMenuMapper;
/*     */ import com.cr.system.service.ISysMenuService;
/*     */ import java.text.MessageFormat;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.LinkedHashMap;
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */
/*     */ import java.util.Set;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.stereotype.Service;
/*     */ 
/*     */ @Service
/*     */ public class SysMenuServiceImpl implements ISysMenuService {
/*     */   public static final String PREMISSION_STRING = "perms[\"{0}\"]";
/*     */   
/*     */   @Autowired
/*     */   private SysMenuMapper menuMapper;
/*     */   
/*     */   @Autowired
/*     */   private SysRoleMenuMapper roleMenuMapper;
/*     */   
/*     */   public List<SysMenu> selectMenusByUser(SysUser user) {
/*  49 */     List<SysMenu> menus = new LinkedList<>();
/*  51 */     if (user.isAdmin()) {
/*  53 */       menus = this.menuMapper.selectMenuNormalAll();
/*     */     } else {
/*  57 */       menus = this.menuMapper.selectMenusByUserId(user.getUserId());
/*     */     } 
/*  59 */     return getChildPerms(menus, 0);
/*     */   }
/*     */   
/*     */   public List<SysMenu> selectMenuList(SysMenu menu, Long userId) {
/*  70 */     List<SysMenu> menuList = null;
/*  71 */     if (SysUser.isAdmin(userId)) {
/*  73 */       menuList = this.menuMapper.selectMenuList(menu);
/*     */     } else {
/*  77 */       menu.getParams().put("userId", userId);
/*  78 */       menuList = this.menuMapper.selectMenuListByUserId(menu);
/*     */     } 
/*  80 */     return menuList;
/*     */   }
/*     */   
/*     */   public List<SysMenu> selectMenuAll(Long userId) {
/*  91 */     List<SysMenu> menuList = null;
/*  92 */     if (SysUser.isAdmin(userId)) {
/*  94 */       menuList = this.menuMapper.selectMenuAll();
/*     */     } else {
/*  98 */       menuList = this.menuMapper.selectMenuAllByUserId(userId);
/*     */     } 
/* 100 */     return menuList;
/*     */   }
/*     */   
/*     */   public Set<String> selectPermsByUserId(Long userId) {
/* 112 */     List<String> perms = this.menuMapper.selectPermsByUserId(userId);
/* 113 */     Set<String> permsSet = new HashSet<>();
/* 114 */     for (String perm : perms) {
/* 116 */       if (MyStringUtils.isNotEmpty(perm))
/* 118 */         permsSet.addAll(Arrays.asList(perm.trim().split(","))); 
/*     */     } 
/* 121 */     return permsSet;
/*     */   }
/*     */   
/*     */   public List<Ztree> roleMenuTreeData(SysRole role, Long userId) {
/* 133 */     Long roleId = role.getRoleId();
/* 134 */     List<Ztree> ztrees = new ArrayList<>();
/* 135 */     List<SysMenu> menuList = selectMenuAll(userId);
/* 136 */     if (MyStringUtils.isNotNull(roleId)) {
/* 138 */       List<String> roleMenuList = this.menuMapper.selectMenuTree(roleId);
/* 139 */       ztrees = initZtree(menuList, roleMenuList, true);
/*     */     } else {
/* 143 */       ztrees = initZtree(menuList, null, true);
/*     */     } 
/* 145 */     return ztrees;
/*     */   }
/*     */   
/*     */   public List<Ztree> menuTreeData(Long userId) {
/* 156 */     List<SysMenu> menuList = selectMenuAll(userId);
/* 157 */     List<Ztree> ztrees = initZtree(menuList);
/* 158 */     return ztrees;
/*     */   }
/*     */   
/*     */   public LinkedHashMap<String, String> selectPermsAll(Long userId) {
/* 169 */     LinkedHashMap<String, String> section = new LinkedHashMap<>();
/* 170 */     List<SysMenu> permissions = selectMenuAll(userId);
/* 171 */     if (MyStringUtils.isNotEmpty(permissions))
/* 173 */       for (SysMenu menu : permissions) {
/* 175 */         section.put(menu.getUrl(), MessageFormat.format("perms[\"{0}\"]", new Object[] { menu.getPerms() }));
/*     */       }  
/* 178 */     return section;
/*     */   }
/*     */   
/*     */   public List<Ztree> initZtree(List<SysMenu> menuList) {
/* 189 */     return initZtree(menuList, null, false);
/*     */   }
/*     */   
/*     */   public List<Ztree> initZtree(List<SysMenu> menuList, List<String> roleMenuList, boolean permsFlag) {
/* 202 */     List<Ztree> ztrees = new ArrayList<>();
/* 203 */     boolean isCheck = MyStringUtils.isNotNull(roleMenuList);
/* 204 */     for (SysMenu menu : menuList) {
/* 206 */       Ztree ztree = new Ztree();
/* 207 */       ztree.setId(menu.getMenuId());
/* 208 */       ztree.setpId(menu.getParentId());
/* 209 */       ztree.setName(transMenuName(menu, permsFlag));
/* 210 */       ztree.setTitle(menu.getMenuName());
/* 211 */       if (isCheck)
/* 213 */         ztree.setChecked(roleMenuList.contains(menu.getMenuId() + menu.getPerms())); 
/* 215 */       ztrees.add(ztree);
/*     */     } 
/* 217 */     return ztrees;
/*     */   }
/*     */   
/*     */   public String transMenuName(SysMenu menu, boolean permsFlag) {
/* 222 */     StringBuffer sb = new StringBuffer();
/* 223 */     sb.append(menu.getMenuName());
/* 224 */     if (permsFlag)
/* 226 */       sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;" + menu.getPerms() + "</font>"); 
/* 228 */     return sb.toString();
/*     */   }
/*     */   
/*     */   public int deleteMenuById(Long menuId) {
/* 240 */     return this.menuMapper.deleteMenuById(menuId);
/*     */   }
/*     */   
/*     */   public SysMenu selectMenuById(Long menuId) {
/* 252 */     return this.menuMapper.selectMenuById(menuId);
/*     */   }
/*     */   
/*     */   public int selectCountMenuByParentId(Long parentId) {
/* 264 */     return this.menuMapper.selectCountMenuByParentId(parentId);
/*     */   }
/*     */   
/*     */   public int selectCountRoleMenuByMenuId(Long menuId) {
/* 276 */     return this.roleMenuMapper.selectCountRoleMenuByMenuId(menuId);
/*     */   }
/*     */   
/*     */   public int insertMenu(SysMenu menu) {
/* 288 */     return this.menuMapper.insertMenu(menu);
/*     */   }
/*     */   
/*     */   public int updateMenu(SysMenu menu) {
/* 300 */     return this.menuMapper.updateMenu(menu);
/*     */   }
/*     */   
/*     */   public String checkMenuNameUnique(SysMenu menu) {
/* 312 */     Long menuId = Long.valueOf(MyStringUtils.isNull(menu.getMenuId()) ? -1L : menu.getMenuId().longValue());
/* 313 */     SysMenu info = this.menuMapper.checkMenuNameUnique(menu.getMenuName(), menu.getParentId());
/* 314 */     if (MyStringUtils.isNotNull(info) && info.getMenuId().longValue() != menuId.longValue())
/* 316 */       return "1"; 
/* 318 */     return "0";
/*     */   }
/*     */   
/*     */   public List<SysMenu> getChildPerms(List<SysMenu> list, int parentId) {
/* 330 */     List<SysMenu> returnList = new ArrayList<>();
/* 331 */     for (Iterator<SysMenu> iterator = list.iterator(); iterator.hasNext(); ) {
/* 333 */       SysMenu t = iterator.next();
/* 335 */       if (t.getParentId().longValue() == parentId) {
/* 337 */         recursionFn(list, t);
/* 338 */         returnList.add(t);
/*     */       } 
/*     */     } 
/* 341 */     return returnList;
/*     */   }
/*     */   
/*     */   private void recursionFn(List<SysMenu> list, SysMenu t) {
/* 353 */     List<SysMenu> childList = getChildList(list, t);
/* 354 */     t.setChildren(childList);
/* 355 */     for (SysMenu tChild : childList) {
/* 357 */       if (hasChild(list, tChild))
/* 359 */         recursionFn(list, tChild); 
/*     */     } 
/*     */   }
/*     */   
/*     */   private List<SysMenu> getChildList(List<SysMenu> list, SysMenu t) {
/* 369 */     List<SysMenu> tlist = new ArrayList<>();
/* 370 */     Iterator<SysMenu> it = list.iterator();
/* 371 */     while (it.hasNext()) {
/* 373 */       SysMenu n = it.next();
/* 374 */       if (n.getParentId().longValue() == t.getMenuId().longValue())
/* 376 */         tlist.add(n); 
/*     */     } 
/* 379 */     return tlist;
/*     */   }
/*     */   
/*     */   private boolean hasChild(List<SysMenu> list, SysMenu t) {
/* 387 */     return (getChildList(list, t).size() > 0);
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-system-4.7.0.jar!\com\cr\system\service\impl\SysMenuServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */